/*********************************************************************
 * INCLUDES
 */
#include "bsp_spi.h"

/*********************************************************************
 * LOCAL DEFINES
 */
SPI_HandleTypeDef hspi2;
DMA_HandleTypeDef HdmaCh1;

/*********************************************************************
 * LOCAL FUNCTIONS
 */

/**
 * @brief Initialize SPI related MSP
 */
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if (hspi->Instance == SPI2)
  {
    /* USER CODE BEGIN SPI2_MspInit 0 */

    /* USER CODE END SPI2_MspInit 0 */
    /* SPI2 clock enable */
    __HAL_RCC_SPI2_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_DMA1_CLK_ENABLE();   /* Enable DMA clock */
    __HAL_RCC_SYSCFG_CLK_ENABLE(); /* Enable SYSCFG clock */

    /**SPI2 GPIO Configuration
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    //    GPIO_InitStruct.Pin = GPIO_PIN_14;
    //    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    //    GPIO_InitStruct.Pull = GPIO_NOPULL;
    //	GPIO_InitStruct.Alternate = GPIO_AF3_SPI2;
    //    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /*GPIO configured as SPI：MISO/MOSI*/
    GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Interrupt configuration */
    HAL_NVIC_SetPriority(SPI2_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(SPI2_IRQn);
    /* DMA_CH1 configuration */
    HdmaCh1.Instance = DMA1_Channel1;
    HdmaCh1.Init.Direction = DMA_MEMORY_TO_PERIPH;
    HdmaCh1.Init.PeriphInc = DMA_PINC_DISABLE;
    HdmaCh1.Init.MemInc = DMA_MINC_ENABLE;
    if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
    {
      HdmaCh1.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
      HdmaCh1.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    }
    else
    {
      HdmaCh1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
      HdmaCh1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    }
    HdmaCh1.Init.Mode = DMA_NORMAL;
    HdmaCh1.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    /* Initialize DMA */
    HAL_DMA_Init(&HdmaCh1);
    /* DMA handle is associated with SPI handle */
    __HAL_LINKDMA(hspi, hdmatx, HdmaCh1);

    /* Set DMA channel map. */
    HAL_DMA_ChannelMap(&HdmaCh1, DMA_CHANNEL_MAP_SPI2_WR); /* SPI2_TX DMA1_CH1 */

    /* DMA interrupt configuration*/
    HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  }
}

/**
 * @brief Deinit SPI MSP
 */
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
{
  if (hspi->Instance == SPI2)
  {
    /* USER CODE BEGIN SPI2_MspDeInit 0 */

    /* USER CODE END SPI2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_SPI2_CLK_DISABLE();

    /**SPI2 GPIO Configuration
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
    HAL_NVIC_DisableIRQ(SPI2_IRQn);
    
    HAL_DMA_DeInit(&HdmaCh1);
    HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn);
  }
}
/*********************************************************************
 * @fn      bsp_spi2_init
 *
 * @brief   Initialize SPI2 peripheral.
 *
 * @return  none
 */
void bsp_spi2_init(void)
{

  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_MASTER;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi2.Init.NSS = SPI_NSS_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  //  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  //  hspi2.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    APP_ErrorHandler();
  }
  /* Initialize SPI peripheral */
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    APP_ErrorHandler();
  }
}

/****************************************************END OF FILE****************************************************/
